<?php
namespace Admin\Models\ims;
use Admin\Models\BaseModel;
use Helpers\Cache;
use Helpers\User;

class XHImsApplyTablet extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iRTId;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sClasses;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sTeachers;


    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iStuNum;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iTchNum;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iState;

    /**
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mStuJson;

    /**
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sRealTch;

    /**
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iSchoolId;

    /**
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iBPPId;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * 教师code
     * @var string
     *
     * create by xjq 20180611
     */
    public $mTchCode;

    /**
     * 学生平板类型
     * @var integer
     * create by xjq 20180612
     */
    public $iProdId;

    /**
     * 收费类型
     *
     * @var integer
     *
     * create by xjq 20180612
     */
    public $iChargeMode;

    /**
     * 备注
     * @var string
     * create by xjq 20180623
     */
    public $mDesc;

    /**
     * 学生
     *
     * @var string
     * create by xjq 20180623
     */
    public $sStudents;


    /**
     * @var string
     *
     * create by xjq 20180626
     */
    public $mTabletShell;

    /**
     * 来源类型 1.流程计划 2. 学生机  3. 教师机 4. 备用机 5.公用机
     *
     * @var integer
     *
     * create by xjq 20180703
     */
    public $iType;

    /**
     * 是否演示机
     *
     * @var integer
     *
     * create by xjq 20180717
     */
    public $bDemo;

    /**
     * 客户id
     *
     * @var integer
     *
     * create by xjq 20180717
     */
    public $iCUId;

    /**
     * 教师忽略扫码信息
     * @var string
     * create by dale 20180831
     */
    public $mTchBackInfo;

    /**
     * 设备机型信息
     *
     * @var integer
     * create by dale 20180831
     */
    public $iSignState;


    public function afterFetch()
    {
        $this->mStuJson = $this->mStuJson ? unserialize($this->mStuJson) : [];
        $this->mTchCode = $this->mTchCode ? unserialize($this->mTchCode) : [];
        $this->mTabletShell = $this->mTabletShell ? unserialize($this->mTabletShell) : [];
        $this->mTchBackInfo = $this->mTchBackInfo ? unserialize($this->mTchBackInfo) : [];
        return true;
    }


    public function beforeSave()
    {
        $this->mStuJson = serialize($this->mStuJson);
        $this->mTchCode = serialize($this->mTchCode);
        $this->mTabletShell = serialize($this->mTabletShell);
        $this->mTchBackInfo = serialize($this->mTchBackInfo);
        if (!$this->iLastModifyUserId) {
            $this->iLastModifyUserId = $this->iCreateUserId;
        }
        return true;
    }


    public function beforeUpdate()
    {
        $this->tLastModifyDate = date('Y-m-d H:i:s');
        $this->iLastModifyUserId = User::getId();
        if (!$this->iLastModifyUserId) {
            $this->iLastModifyUserId = $this->iCreateUserId;
        }
        return true;
    }



    /**
     * Initialize method for model.
     */
//    public function initialize()
//    {
//        $this->setSource("XHIms_ApplyTablet");
//    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHIms_ApplyTablet';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHImsApplyTablet[]|XHImsApplyTablet|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHImsApplyTablet|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }


    public function save($data = null, $whiteList = null, $recordUpdateInfo = true)
    {
        if ($recordUpdateInfo) {
            parent::beforeUpdate();
        } else {
            $this->beforeUpdate();
        }
        return parent::save($data, $whiteList);
    }

//    public function beforeUpdate()
//    {
//        return true;
//    }


}
